#!/run/current-system/profile/bin/env bash

NO_COLOR="\033[0m"
UNDERSCORE="\033[4m"
BLINK="\033[5m"
BLACK="\033[0;30m"
DARK_GRAY="\033[1;30m"
RED="\033[0;31m"
LIGHT_RED="\033[1;31m"
GREEN="\033[0;32m"
LIGHT_GREEN="\033[1;32m"
BROWN="\033[0;33m"
YELLOW="\033[1;33m"
BLUE="\033[0;34m"
LIGHT_BLUE="\033[1;34m"
PURPLE="\033[0;35m"
LIGHT_PURPLE="\033[1;35m"
CYAN="\033[0;36m"
LIGHT_CYAN="\033[1;36m"
GRAY="\033[0;37m"
WHITE="\033[1;37m"

###############################################################################
# Env
SD_SCRIPT_PATH=$(readlink -f "$0")
SD_SCRIPT_DIR=`dirname ${SD_SCRIPT_PATH}`
SD_SCRIPT_NAME=`basename ${SD_SCRIPT_PATH}`

# source ${SD_SCRIPT_DIR}/script/utils.sh

###############################################################################
set -eE
trap 'printf "\n${NO_COLOR}Error: in ${RED}$0${NO_COLOR} on line ${RED}$LINENO${NO_COLOR}\n"' ERR
set -o pipefail

###############################################################################
#
anykey() {
    printf "${NO_COLOR}Press any key to continue...${NO_COLOR}"
    read -s -n 1  # -s do not echo input；-n 1 only read 1 character
    echo ""
}

echo_tip() {
    printf "\n${LIGHT_CYAN}[*] ${CYAN}$*${NO_COLOR}\n"
}

echo_cmd1() {
    local SUDO=""
    if [ "$1" = "sudo" ] || [ "$1" = "doas" ] || [ "$1" = "run0" ] || [ "$1" = "export" ]; then
        SUDO="$1 "
        shift 1
    fi
    local CMD="$1"
    shift 1
    local ARGS="$@"

    printf "[`uname -n`]=> ${YELLOW}$SUDO${LIGHT_CYAN}${CMD} ${LIGHT_BLUE}${ARGS}${NO_COLOR}\n"
}

echo_cmd() {
    local SUDO=""
    if [ "$1" = "sudo" ] || [ "$1" = "doas" ] || [ "$1" = "run0" ] || [ "$1" = "export" ]; then
        SUDO="$1 "
        shift 1
    fi
    local CMD="$1"
    shift 1

    # 重建参数，对包含空格的参数添加引号
    local ARGS=""
    for arg in "$@"; do
        # 检查参数是否包含空格
        if [[ "$arg" == *" "* ]]; then
            ARGS="$ARGS \"$arg\""  # 为包含空格的参数添加双引号
        else
            ARGS="$ARGS $arg"      # 不包含空格则直接追加
        fi
    done
    ARGS="${ARGS:1}"  # 移除开头的空格

    printf "[`uname -n`]=> ${YELLOW}$SUDO${LIGHT_CYAN}${CMD} ${LIGHT_BLUE}${ARGS}${NO_COLOR}\n"
}

RUN_CMD() {
    echo_cmd "$@"
    eval $(printf "%q " "$@")
}

RUN_STEP() {
    local oldir=$PWD
    local ARGS="$@"
    echo ""
    printf "${CYAN}<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>${NO_COLOR}\n"
    local STRING=${1//__/\:\ }
    STRING=${STRING//_/\ }
    shift
    printf "${CYAN}<> %-65s${NO_COLOR}${LIGHT_CYAN}[Host]${NO_COLOR}${CYAN}  <>${NO_COLOR}\n" "${STRING} | args:$*"
    printf "${CYAN}<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>${NO_COLOR}\n"
    anykey
    echo "-----------------eval ${ARGS}"
    eval "${ARGS}"
    cd $oldir
}

sudo_program() {
    if [ "`whoami`" = "root" ]; then
        echo ""
    elif [ "`command -v sudo`" != "" ]; then
        echo "sudo"
    elif [ "`command -v doas`" != "" ]; then
        echo "doas"
    elif [ "`command -v run0`" != "" ]; then
        echo "run0"
    else
        echo ""
    fi
}
###############################################################################
#

guix__write_channels_scm() {
    local GUIX_CHANNELS_CONF="$1"
    local SUDO=$2
    RUN_CMD ${SUDO} mkdir -p $(dirname ${GUIX_CHANNELS_CONF})

    ${SUDO} tee ${GUIX_CHANNELS_CONF} <<EOF
(cons*
    (channel
        (inherit (car %default-channels))
        ;(url "https://git.savannah.gnu.org/git/guix.git")) ; offical
        (url "https://git.nju.edu.cn/nju/guix.git"))        ; mirror

    (channel
        (name 'guix-hpc)
        (url "https://gitlab.inria.fr/guix-hpc/guix-hpc.git"))

    ;(channel
    ;    (name 'nonguix)
    ;    (url "https://gitee.com/DEV-READY/nonguix")
    ;    (branch "master"))
    ;    ;(commit "305a0832df9bffe8de5814fef0f29fd8e6f83a34")

    (channel
        (name 'nonguix)
        ;(url "https://gitlab.com/nonguix/nonguix")
        (url "https://codeberg.org/nonguix/nonguix-mirror")
        (introduction
            (make-channel-introduction "897c1a470da759236cc11798f4e0a5f7d4d59fbc"
                (openpgp-fingerprint "2A39 3FFF 68F4 EF7A 3D29  12AF 6F51 20A0 22FB B2D5"))))

    (channel
        (name 'guix-gaming-games)
        (url "https://gitlab.com/guix-gaming-channels/games.git")
        (introduction
            (make-channel-introduction "c23d64f1b8cc086659f8781b27ab6c7314c5cca5"
                (openpgp-fingerprint "50F3 3E2E 5B0C 3D90 0424  ABE8 9BDC F497 A4BB CC7F"))))

    %default-channels)

EOF
}

guix__write_config_scm() {
    local PATH_CONFIG_SCM=$1
    local HOSTNAME=$2
    local USERNAME=$3
    local SUDO=$4
    
    RUN_CMD ${SUDO} mkdir -p $(dirname ${PATH_CONFIG_SCM})

    [[ "${HOSTNAME}" = "" ]] && read -p "Please input hostname: " HOSTNAME
    [[ "${USERNAME}" = "" ]] && read -p "Please input username: " USERNAME

    [[ "${HOSTNAME}" = "" ]] && HOSTNAME="guixos"
    [[ "${USERNAME}" = "" ]] && USERNAME="guix"

    local PASSWORD='$6$7Htdy5eAsAvMEnJw$T1osUscgXPVdxDSgLPv0csmnJes3.bBdbcIx3TwvEqbBeHYPAY07iPpQr7kjrqDdV3JV7O2Zaj0PsI1twWpDK1'
    if command -v mkpasswd > /dev/null 2>&1; then
        echo "Please input pwssword for 'root' and '${USERNAME}': "
        PASSWORD=$(mkpasswd -m sha-512)
    else
        echo "'root' and '${USERNAME}' default password: guix"
    fi

    #1) start
    ${SUDO} tee ${PATH_CONFIG_SCM} <<EOF
;; This is an operating system configuration generated
;; by the graphical installer.
;;
;; Once installation is complete, you can learn and modify
;; this file to tweak the system configuration, and pass it
;; to the 'guix system reconfigure' command to effect your
;; changes.


;; Indicate which modules to import to access the variables
;; used in this configuration.
(use-modules (gnu)
             ;(gnu services)           ; 提供基础服务定义
             ;(gnu services base)      ; 提供 kernel-module-loader-service-type
             ;(gnu packages gnome)    ; 提供 GNOME 相关包定义
             (gnu services desktop)  ; 提供桌面服务
             (gnu services xorg)     ; 提供 Xorg 服务
             (nongnu packages linux)
             (nongnu system linux-initrd)
             (nongnu packages nvidia)
             (nongnu services nvidia))
(use-service-modules cups desktop networking ssh xorg)

(operating-system
  (locale "en_US.utf8")
  (timezone "Asia/Shanghai")
  (keyboard-layout (keyboard-layout "us"))
  (host-name "${HOSTNAME}")

  ;; root password
  ;(root-password "${PASSWORD}")

  ;; The list of user accounts ('root' is implicit).
  (users (cons* (user-account
                  (name "${USERNAME}")
                  (comment "${USERNAME}")
                  (group "users")
                  (home-directory "/home/${USERNAME}")
                  (password "${PASSWORD}")
                  (supplementary-groups '("wheel" "netdev" "audio" "video")))
                %base-user-accounts))

  ;; Non-free Linux kernel and firmware
  (kernel linux)
EOF

    if [ "$NVIDIA" = "1" ]; then
        ${SUDO} tee -a ${PATH_CONFIG_SCM} <<EOF
  (kernel-loadable-modules (list nvidia-module))
  ;(kernel-arguments '("loglevel=1,quiet"))
  ;(kernel-arguments '("modprobe.blacklist=nouveau"
  ;                    ;; Set this if the card is not used for displaying or
  ;                    ;; you're using Wayland:
  ;                    "nvidia_drm.modeset=1"))
  ;; Blacklisting Nouveau so Intel i915 will be used (works better atm).
  (kernel-arguments
    (append (list (string-append "modprobe.blacklist=bluetooth,btrtl,btintel,"
                                 "btcm,btusb,nouveau,pcspkr")
                  ;; I don't know why I have this:
                  "nvidia.NVreg_RegistryDwords=\"OverrideMaxPerf=0x0\"")))
EOF
    fi

    ${SUDO} tee -a ${PATH_CONFIG_SCM} <<EOF
  ;(firmware (list linux-firmware))
  ;(firmware (cons* iwlwifi-firmware %base-firmware))
  (firmware (cons* linux-firmware iwlwifi-firmware %base-firmware))
  (initrd microcode-initrd)

  ;; Packages installed system-wide.  Users can also install packages
  ;; under their own account: use 'guix search KEYWORD' to search
  ;; for packages and 'guix install PACKAGE' to install a package.
  (packages (append (list (specification->package "git")
                          (specification->package "whois")
                          (specification->package "ncurses")

                          (specification->package "ibus")
                          (specification->package "ibus-rime")
                          (specification->package "ibus-libpinyin")

                          ;(specification->package "firefox")
                          ;(specification->package "steam")
                          (specification->package "podman")
                          (specification->package "flatpak")
                          ;(specification->package "snap")
                          (specification->package "peek")
                          ;(specification->package "gnome-boxes")
                          (specification->package "gnome-tweaks")
                          (specification->package "gnome-shell-extension-dash-to-panel")
                          (specification->package "gnome-shell-extension-customize-ibus")

                          (specification->package "font-wqy-microhei")
                          (specification->package "font-wqy-zenhei")
                          (specification->package "font-lxgw-wenkai")
                          (specification->package "font-google-noto-serif-cjk")
                          (specification->package "font-google-noto-sans-cjk")
                          (specification->package "font-cns11643")
                          (specification->package "font-adobe-source-han-sans")
                          (specification->package "font-sarasa-gothic")
                          (specification->package "font-plangothic")
EOF

    if [ "$NVIDIA" = "1" ]; then
        ${SUDO} tee -a ${PATH_CONFIG_SCM} <<EOF

                          (specification->package "brightnessctl")
                          (specification->package "dxvk")
                          (specification->package "mesa-utils")
                          (specification->package "vulkan-tools")

EOF
    fi

    ${SUDO} tee -a ${PATH_CONFIG_SCM} <<EOF
                          ;(specification->package "icewm")
                          (specification->package "i3-wm")
                          (specification->package "i3status")
                          (specification->package "dmenu")
                          (specification->package "st")
                          (specification->package "ratpoison")
                          (specification->package "xterm")
                          (specification->package "emacs")
                          (specification->package "emacs-exwm")
                          (specification->package
                           "emacs-desktop-environment")) %base-packages))

  ;; Below is the list of system services.  To search for available
  ;; services, run 'guix system search KEYWORD' in a terminal.
  (services (modify-services
EOF

    if [ "$NVIDIA" = "1" ]; then
        ${SUDO} tee -a ${PATH_CONFIG_SCM} <<EOF
   (append (list (service nvidia-service-type)
                 ;; Configure desktop environment, GNOME for example.
                 (service gnome-desktop-service-type)
                 ;(service gnome-desktop-service-type
                 ;         ;; Enable NVIDIA support, only do this when the card is
                 ;         ;; used for displaying.
                 ;         (gnome-desktop-configuration
                 ;           (gnome (replace-mesa gnome))))
                 ;(service xfce-desktop-service-type)
                 ;(service mate-desktop-service-type)
                 ;(service enlightenment-desktop-service-type)
                 ;(specification->package "icewm")

                 ;; To configure OpenSSH, pass an 'openssh-configuration'
                 ;; record as a second argument to 'service' below.
                 (service openssh-service-type)
                 (service tor-service-type)
                 (service cups-service-type)
                 (set-xorg-configuration
                  (xorg-configuration 
                    (modules (cons nvda %default-xorg-modules))
                    (drivers '("nvidia"))
                    (keyboard-layout keyboard-layout))))

           ;; This is the default list of services we
           ;; are appending to.
           %desktop-services)

	(guix-service-type config => (guix-configuration
					(inherit config)
					(substitute-urls '("${SUBSTITUTE_MIRROS}"))
					(authorized-keys
                      (append (list (local-file "signing-key.pub"))
                        %default-authorized-guix-keys))))))

EOF
    else
        ${SUDO} tee -a ${PATH_CONFIG_SCM} <<EOF
   (append (list (service gnome-desktop-service-type)
                 ;(service xfce-desktop-service-type)
                 ;(service mate-desktop-service-type)
                 ;(service enlightenment-desktop-service-type)
                 ;(specification->package "icewm")

                 ;; To configure OpenSSH, pass an 'openssh-configuration'
                 ;; record as a second argument to 'service' below.
                 (service openssh-service-type)
                 (service tor-service-type)
                 (service cups-service-type)
                 (set-xorg-configuration
                  (xorg-configuration (keyboard-layout keyboard-layout))))

           ;; This is the default list of services we
           ;; are appending to.
           %desktop-services)

    (guix-service-type config =>
      (guix-configuration
       (inherit config)
       (substitute-urls '("https://mirror.sjtu.edu.cn/guix" "https://substitutes.nonguix.org"))
       ;(mirrors
       ; (list
       ;  ;; 为 kernel.org 设置多个镜像源
       ;  (mirror
       ;   (name "kernel.org")
       ;   (url "https://mirrors.ustc.edu.cn/kernel.org/")
       ;   (hooks (list (lambda (file url)
       ;                  (string-append file)))))
       ;  (mirror
       ;   (name "kernel.org")
       ;   (url "https://mirrors.nju.edu.cn/kernel.org/")
       ;   (hooks (list (lambda (file url)
       ;                  (string-append file)))))))
       (authorized-keys
        (append (list (local-file "signing-key.pub"))
                %default-authorized-guix-keys))))
    ;; other
))

EOF
    fi

    #2) BIOS/EFI
    if [ -e "/sys/firmware/efi" ]; then
        #2.1) EFI mode
        ${SUDO} tee -a ${PATH_CONFIG_SCM} <<EOF
  (bootloader (bootloader-configuration
                (bootloader grub-efi-bootloader)
                (targets (list "/boot/efi"))
EOF
    else
        #2.2) BIOS mode
        ${SUDO} tee -a ${PATH_CONFIG_SCM} <<EOF
  (bootloader (bootloader-configuration
                (bootloader grub-bootloader)
                (targets (list "${DEV_DISK}"))
EOF
    fi

    #3) keyboard-layout
    ${SUDO} tee -a ${PATH_CONFIG_SCM} <<EOF
                (keyboard-layout keyboard-layout)))
  (initrd-modules (append '("mptspi") %base-initrd-modules))

EOF

    #4) swap
    if [ -e ${DEV_SWAP} ]; then
        ${SUDO} tee -a ${PATH_CONFIG_SCM} <<EOF
  (swap-devices (list (swap-space (target "${DEV_SWAP}"))))

EOF
    fi

    #5) /boot/efi /boot /
    ${SUDO} tee -a ${PATH_CONFIG_SCM} <<EOF
  ;; The list of file systems that get "mounted".  The unique
  ;; file system identifiers there ("UUIDs") can be obtained
  ;; by running 'blkid' in a terminal.
  (file-systems (cons* (file-system
                         (mount-point "/")
                         (device "${DEV_ROOT}")
                         (type "xfs"))
                       (file-system
                         (mount-point "/boot")
                         (device "${DEV_BOOT}")
                         (type "xfs"))
                       (file-system
                         (mount-point "/boot/efi")
                         (device "${DEV_EFI}")
                         (type "vfat"))
EOF

    #6) /home
    if [ -e ${DEV_HOME} ]; then
        ${SUDO} tee -a ${PATH_CONFIG_SCM} <<EOF
                       (file-system
                         (mount-point "/home")
                         (device "${DEV_HOME}")
                         (type "xfs"))
EOF
    fi

    #7) end
    ${SUDO} tee -a ${PATH_CONFIG_SCM} <<EOF
                    %base-file-systems)))
EOF
}

STEP__format() {
    local SUDO=$(sudo_program)
    read -p "mkfs.xfs? [y/N]: " NEED_MKFS
    echo
    if echo "${NEED_MKFS}" | grep -iq "^y"; then
	    # 1.1 make rootfs with zfs
	    #RUN_CMD zpool create -o ashift=12 -O acltype=posixacl -O canmount=off -O dnodesize=auto -O relatime=on -O xattr=sa -O mountpoint=/ -R ${MOUNT_ROOT} rpool ${DEV_ROOT}
	    #RUN_CMD zfs create -o canmount=off -o mountpoint=none rpool/ROOT
	    #RUN_CMD zfs create -o canmount=noauto -o mountpoint=/ rpool/ROOT/chimera
	    #RUN_CMD zfs mount rpool/ROOT/chimera

	    # 1.2 make rootfs with xfs
        [[ -e ${DEV_ROOT} ]] && RUN_CMD ${SUDO} mkfs.xfs -f ${DEV_ROOT}
        [[ -e ${DEV_EFI} ]] && RUN_CMD ${SUDO} mkfs.vfat ${DEV_EFI}
        [[ -e ${DEV_BOOT} ]] && RUN_CMD ${SUDO} mkfs.xfs -f ${DEV_BOOT}
        [[ -e ${DEV_HOME} ]] && RUN_CMD ${SUDO} mkfs.xfs -f ${DEV_HOME}
        [[ -e ${DEV_SWAP} ]] && RUN_CMD ${SUDO} mkswap ${DEV_SWAP}
    fi
}

STEP__mount() {
    local SUDO=$(sudo_program)
    read -p "mount? [y/N]: " NEED_MOUNT
    echo
    if echo "${NEED_MOUNT}" | grep -iq "^y"; then
        echo_tip "#1 mount /root"
        RUN_CMD ${SUDO} mkdir -p ${MNT_ROOT}
        RUN_CMD ${SUDO} chmod 755 ${MNT_ROOT}
        RUN_CMD ${SUDO} mount ${DEV_ROOT} ${MNT_ROOT}

        echo_tip "#2 mount /root/boot"
        RUN_CMD ${SUDO} mkdir -p ${MNT_BOOT}
        RUN_CMD ${SUDO} mount ${DEV_BOOT} ${MNT_BOOT}

        echo_tip "#3 mount /root/boot/efi"
        RUN_CMD ${SUDO} mkdir -p ${MNT_EFI}
        RUN_CMD ${SUDO} chmod 700 ${MNT_EFI}
        RUN_CMD ${SUDO} mount ${DEV_EFI} ${MNT_EFI}

        echo_tip "#4 mount /home"
        if [ -e ${DEV_HOME} ]; then
            RUN_CMD ${SUDO} mkdir -p ${MNT_HOME}
            RUN_CMD ${SUDO} mount ${DEV_HOME} ${MNT_HOME}
        fi

        echo_tip "#5 mount necessary virtual file systems"
        RUN_CMD ${SUDO} mkdir -p /mnt/proc
        RUN_CMD ${SUDO} mount --types proc /proc /mnt/proc

        RUN_CMD ${SUDO} mkdir -p /mnt/sys
        RUN_CMD ${SUDO} mount --rbind /sys /mnt/sys

        RUN_CMD ${SUDO} mkdir -p /mnt/dev
        RUN_CMD ${SUDO} mount --rbind /dev /mnt/dev
        RUN_CMD ${SUDO} mkdir -p /mnt/dev/pts
        RUN_CMD ${SUDO} mount --rbind /dev/pts /mnt/dev/pts
    fi
}

STEP__cow_store() {
    echo_tip "start cow-store"
    RUN_CMD ${SUDO} herd start cow-store ${MNT_ROOT}
}

# sjtu-old: https://mirrors.sjtug.sjtu.edu.cn/guix
# sjtu-new: https://mirror.sjtu.edu.cn/guix
STEP__config() {
    local SUDO=$(sudo_program)
    local ROOT_PATH=""
    if [ -e "${MNT_ROOT}/run" ]; then
        ROOT_PATH="${MNT_ROOT}"
    fi

    RUN_CMD ${SUDO} cp -f ${SD_SCRIPT_PATH} ${ROOT_PATH}/bin/

    echo_tip "#1 gen channels.scm"
    #local CHANNELS_SCM=$HOME/.config/guix/channels.scm
    #guix__write_channels_scm ${CHANNELS_SCM}

    local CHANNELS_SCM=/etc/guix/channels.scm
    guix__write_channels_scm ${CHANNELS_SCM} ${SUDO}
    [[ "${ROOT_PATH}" != "" ]] && guix__write_channels_scm ${ROOT_PATH}/etc/guix/channels.scm ${SUDO}

    echo_tip "#2 update sources"
    read -p "guix pull && guix upgrade? [y/N]: " NEED_PULL
    echo
    if echo "${NEED_PULL}" | grep -iq "^y"; then
        echo_tip GUIX_PACKAGE_MIRRORS="${GUIX_PACKAGE_MIRRORS}"
        RUN_CMD guix pull --allow-downgrades --substitute-urls="${SUBSTITUTE_MIRROS}"
        RUN_CMD guix upgrade --substitute-urls="${SUBSTITUTE_MIRROS}"
    fi

    echo_tip "#3 export GUIX_PROFILE and PATH"
    export GUIX_PROFILE="$HOME/.config/guix/current"
    . "$GUIX_PROFILE/etc/profile"
    export PATH="$HOME/.config/guix/current/bin/guix:$PATH"

    echo_tip "#4 describe"
    #RUN_CMD guix describe
    RUN_CMD guix system describe

    echo_tip "#5 gen config.scm"
    local CONFIG_SCM=${ROOT_PATH}/etc/config.scm
    local NONGUIX_SIGNKEY_PUB=${ROOT_PATH}/etc/signing-key.pub
    ${SUDO} wget -O "${NONGUIX_SIGNKEY_PUB}" "https://substitutes.nonguix.org/signing-key.pub"
    guix__write_config_scm ${CONFIG_SCM} "guixos" "damon" ${SUDO}

    read -p "edit ${CONFIG_SCM}? [y/N]: " NEED_EDIT
    echo
    if echo "${NEED_EDIT}" | grep -iq "^y"; then
        RUN_CMD ${SUDO} nano ${CONFIG_SCM}
    fi
}

STEP__bootstrap() {
    echo_tip "install system to disk"
    local SUDO=$(sudo_program)
    local ROOT_PATH=""
    if [ -e "${MNT_ROOT}/run" ]; then
        ROOT_PATH="${MNT_ROOT}"
    fi

    echo_tip GUIX_PACKAGE_MIRRORS="${GUIX_PACKAGE_MIRRORS}"

    local CONFIG_SCM=${ROOT_PATH}/etc/config.scm
    read -p "install guixos?(y:bootstrap n:reconfigure) [y/N]: " INPUT
    echo
    if echo "${INPUT}" | grep -iq "^y"; then
        RUN_CMD ${SUDO} guix system init ${CONFIG_SCM} ${MNT_ROOT} --allow-downgrades --substitute-urls="${SUBSTITUTE_MIRROS}"
    else
        # RUN_CMD ${SUDO} cp -f ${CONFIG_SCM} ${ROOT_PATH}/etc/
        RUN_CMD ${SUDO} guix system reconfigure ${CONFIG_SCM} --allow-downgrades --substitute-urls="${SUBSTITUTE_MIRROS}"
    fi
}

guix__status() {
    cat /run/current-system/configuration.scm
    guix system describe
    guix package --list-installed
}

###############################################################################
if [ $1 = sda ]; then
    DEV_DISK=/dev/sda

    DEV_EFI=/dev/sda1
    DEV_BOOT=/dev/sda2
    DEV_ROOT=/dev/sda3
    DEV_HOME=/dev/sda4
    DEV_SWAP=/dev/sda5

elif [ $1 = nvme ]; then
    DEV_DISK=/dev/nvme0n1

    DEV_EFI=/dev/nvme0n1p1
    DEV_BOOT=/dev/nvme0n1p2
    DEV_ROOT=/dev/nvme0n1p3
    DEV_HOME=/dev/nvme0n1p4
    DEV_SWAP=/dev/nvme0n1p5
fi

MNT_ROOT=/mnt/guix
MNT_BOOT=${MNT_ROOT}/boot
MNT_EFI=${MNT_ROOT}/boot/efi
MNT_HOME=${MNT_ROOT}/home

#SUBSTITUTE_MIRROS="https://mirror.sjtu.edu.cn/guix https://ci.guix.gnu.org https://bordeaux.guix.gnu.org"
#SUBSTITUTE_MIRROS="https://mirrors.sjtug.sjtu.edu.cn/guix https://ci.guix.gnu.org https://bordeaux.guix.gnu.org"
SUBSTITUTE_MIRROS="https://mirror.sjtu.edu.cn/guix https://substitutes.nonguix.org"

NVIDIA=0
#RUN_CMD export GUIX_PACKAGE_MIRRORS="mirror://kernel.org/=https://mirrors.ustc.edu.cn/kernel.org/"
export GUIX_PACKAGE_MIRRORS="\
mirror://kernel.org/linux/kernel/firmware/=https://mirrors.nju.edu.cn/kernel.org/linux/kernel/firmware/ \
mirror://kernel.org/=https://mirrors.ustc.edu.cn/kernel.org/ \
mirror://gnu/=https://mirrors.ustc.edu.cn/gnu/ \
mirror://kde/=https://mirrors.ustc.edu.cn/kde/ \
https://cpan.metacpan.org/=https://mirrors.ustc.edu.cn/CPAN/ \
https://pypi.org/=https://mirrors.ustc.edu.cn/pypi/ \
https://pypi.org/pypi/=https://mirrors.ustc.edu.cn/pypi/simple/ \
https://files.pythonhosted.org/=https://mirrors.ustc.edu.cn/pypi/"

export GUIX_SOURCE_URL_OVERRIDES='
(
  ("mirror://kernel.org/.*" 
   "https://mirrors.nju.edu.cn/kernel.org/" 
   "https://cdn.kernel.org/pub/")
)
'

if [ "$2" = "conf" ] || [ "$2" = "config" ]; then
    echo_tip "CONFIG MODE"
    RUN_STEP STEP__config
    RUN_STEP STEP__bootstrap
else
    cfdisk ${DEV_DISK}
    echo_tip "INSTALL MODE: ${DEV_DISK}"
    RUN_STEP STEP__format
    RUN_STEP STEP__mount
    RUN_STEP STEP__cow_store
    RUN_STEP STEP__config
    RUN_STEP STEP__bootstrap
fi

